package com.neutech.base;

import com.neutech.running.BackGround;
import com.neutech.running.Bird;
import com.neutech.running.Land;
import com.neutech.running.Pencil;
import com.neutech.running.PencilDown;
import com.neutech.running.PencilUp;
import com.neutech.running.StartButton;
import com.neutech.util.ImageUtil;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

public class GameFrame extends Frame {

    public GameFrame(){
        init();
    }
    /**
     *  窗口初始化
     * */
    public void init(){
        //设置窗口尺寸
        setSize(Constant.WINDOW_WIDTH,Constant.WINDOW_HEIGHT);
        //设置窗口居中
        setLocationRelativeTo(null);
        //禁止改变窗口大小
        setResizable(false);
        //禁止输入法
        enableInputMethods(false);
        //设置显示
        setVisible(true);

        //设置窗口关闭功能
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        //设置一个定时刷新
        new Thread(){
            @Override
            public void run(){
                while(true){
                    //调用重绘
                    repaint();
                    try {
                        Thread.sleep(1000/Constant.FPS);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }.start();

        //添加事件
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(gameStatusEnum==GameStatusEnum.RUNNING){
                    bird.reset();
                }else if(gameStatusEnum==GameStatusEnum.FAILED){
                    //重开
                    reStart();

                }

            }
        });

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_SPACE);
            }
        });



        createPencil();
    }
    BackGround background=new BackGround();
    Bird bird=new Bird(Constant.WINDOW_WIDTH/6,Constant.WINDOW_HEIGHT/2);

    private GameStatusEnum gameStatusEnum=GameStatusEnum.RUNNING;
    Land land=new Land();
    public List<Pencil> pencilList=new CopyOnWriteArrayList<>();
    StartButton sb=new StartButton();

    //渲染页面
    @Override
    public void paint(Graphics g) {
        if(gameStatusEnum==GameStatusEnum.RUNNING){
            //绘画背景图
            background.draw(g);
            //显示铅笔
            isAddPencil();
            for (Pencil pencil : pencilList) {
                pencil.move();
                pencil.draw(g);
            }

            land.move();
            land.draw(g);

            bird.move();
            bird.draw(g);
            birdCollisionLand();
            birdCollisionPencil();
        }else if(gameStatusEnum==GameStatusEnum.FAILED){
            //绘画背景图
            background.draw(g);
            //显示铅笔
            for (Pencil pencil : pencilList) {
                pencil.draw(g);
            }

            land.draw(g);

            bird.draw(g);

            sb.draw(g);
        }


    }

    //碰撞检测
    //小鸟碰撞陆地
    public void birdCollisionLand(){
        if(bird.getY()+ bird.getHeight() >= land.getY()){
            gameStatusEnum=GameStatusEnum.FAILED;
        }
    }
    //小鸟碰撞铅笔
    public void birdCollisionPencil(){
        for (Pencil pencil : pencilList) {
            if(pencil.createRectangle().intersects(bird.createRectangle())){
                gameStatusEnum=GameStatusEnum.FAILED;
            }
        }

    }

    public void reStart(){

        pencilList.clear();
        createPencil();
        bird.setY(Constant.WINDOW_HEIGHT/2);
        bird.resetSelf();
        gameStatusEnum=GameStatusEnum.RUNNING;
    }


    Random random=new Random();
    public void isAddPencil() {
        if (pencilList.size() == 2 && pencilList.get(0).getX() <= (Constant.WINDOW_WIDTH / 2 - 26)) {
            createPencil();
        }
    }

    //创建一对铅笔
    public void createPencil(){
        //最小的top值
        int minTop=Constant.WINDOW_HEIGHT/8 +Constant.WINDOW_NAV;
        //最大的top值
        int maxTop=Constant.WINDOW_HEIGHT/2;
        int gap=(Constant.WINDOW_HEIGHT / 5)+(Constant.WINDOW_NAV / 2);
        int top = random.nextInt(maxTop-minTop)+minTop;

        //添加一对铅笔
        pencilList.add(new PencilUp(Constant.WINDOW_WIDTH,
                top- ImageUtil.get("pie_up").getHeight(null)));
        pencilList.add(new PencilDown(Constant.WINDOW_WIDTH,top+gap));
    }


    //解决窗口闪烁问题
    private Image image;
    public void update(Graphics g) {
        // 截取窗体所在位置的图片
        if (image == null) {
            image = this.createImage(this.getWidth(), this.getHeight());
        }
        // 获得截取图片的画布
        Graphics gre = image.getGraphics();
        // 获取画布的底色并且使用这种颜色填充画布（默认的颜色为黑色）
        gre.setColor(gre.getColor());
        // 有清除上一步图像的功能，相当于gImage.clearRect(0, 0, WIDTH, HEIGHT)
        gre.fillRect(0, 0, this.getWidth(), this.getHeight());
        // 将截下的图片上的画布传给重绘函数，重绘函数只需要在截图的画布上绘制即可，不必在从底层绘制
        paint(gre);
        //将接下来的图片加载到窗体画布上去，才能考到每次画的效果
        g.drawImage(image, 0, 0, null);
    }




}

